<?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');


    class Region extends DAO {

        private static $instance ;

        public static function newInstance() {
            if(!self::$instance instanceof self) {
                self::$instance = new self ;
            }
            return self::$instance ;
        }

        public function getTableName() { return DB_TABLE_PREFIX . 't_region'; }

        public function getByCountry($country_id) {
            return $this->conn->osc_dbFetchResults("SELECT * FROM %s WHERE fk_c_country_code = '%s' ORDER BY s_name ASC", $this->getTableName(), addslashes($country_id));
        }

        public function findByNameAndCode($name, $code) {
            return $this->conn->osc_dbFetchResult("SELECT * FROM %s WHERE s_name = '%s' AND fk_c_country_code = '%s' LIMIT 1", $this->getTableName(), addslashes($name), addslashes($code));
        }

        public function findByName($name) {
            return $this->conn->osc_dbFetchResult("SELECT * FROM %s WHERE s_name = '%s' LIMIT 1", $this->getTableName(), addslashes($name));
        }

        public function ajax($query, $country = null) {
            $country_sql = ($country != null) ? ' AND fk_c_country_code LIKE \'' . addslashes(strtolower($country)) . '\' ' : '';
            return $this->conn->osc_dbFetchResults("SELECT pk_i_id as id, s_name as label, s_name as value FROM %s WHERE s_name LIKE '%s%%' %s LIMIT 5", $this->getTableName(), addslashes($query), $country_sql);
        }

        public function findByNameOnCountry($query, $country) {
            $country_sql = ($country != null) ? ' AND fk_c_country_code LIKE \'' . addslashes(strtolower($country)) . '\' ' : '';
            return $this->conn->osc_dbFetchResult("SELECT pk_i_id, s_name, s_name FROM %s WHERE `s_name` LIKE '%s' %s LIMIT 1", $this->getTableName(), addslashes($query), $country_sql);
        }
    }

?>